/* ===========================================================================================
 * h5_nav.js v1.1.0
 * ===========================================================================================
 * Original written by Ysy
 * ===========================================================================================
 * Date 12/05/2020
 * ========================================================================================== */


(function (factory) {
    "use strict";
    if (typeof define === "function" && define.amd) {
        define(["jquery"], factory);
    } else {
        factory(jQuery);
    }
}(function ($) {

    let Navbarscroll = function (element, options) {
        this.options = $.extend({}, $.fn.navbarscroll.defaults, options);
        this.$element = $(element);
        this.render();
    };

    Navbarscroll.prototype = {

        constructor: Navbarscroll,

        render: function () {// 渲染

            $.each(this.$element, (index, value) => {
                let _wrapper = $(value);
                let _win = $(window);
                let _win_width = _win.width(), _wrapper_width = _wrapper.width(),
                    _wrapper_off_left = _wrapper.offset().left;
                let _wrapper_off_right = _win_width - _wrapper_off_left - _wrapper_width;
                let _obj_scroller = _wrapper.children(".scroller");
                let _obj_ul = _obj_scroller.children("ul");
                let _obj_li = _obj_ul.children("li");
                let _scroller_w = 0;

                _obj_li.css({"margin-left": "0", "margin-right": "0"});

                $.each(_obj_li, (index, value) => {
                    _scroller_w += value.offsetWidth;
                });

                _obj_scroller.width(_scroller_w + this.options.scrollerWidth);

                let myScroll = new IScroll("#" + _wrapper.attr("id"), {
                    eventPassthrough: true,
                    scrollX: true,
                    scrollY: false,
                    preventDefault: false
                });

                this.init(_obj_li.eq(this.options.defaultSelect), myScroll, _win_width, _scroller_w, _wrapper_width, _wrapper_off_right, _obj_li, _wrapper_off_left);

                _obj_li.click((e) => {
                    this.init($(e.target).closest("li"), myScroll, _win_width, _scroller_w, _wrapper_width, _wrapper_off_right, _obj_li, _wrapper_off_left);
                });

                _wrapper[0].addEventListener("touchmove", function (e) {
                    e.preventDefault();
                }, false);
            });
        },

        init: function (element, myScroll, _win_width, _scroller_w, _wrapper_width, _wrapper_off_right, _obj_li, _wrapper_off_left) {// 渲染

            let $this_obj = element;
            let duibi = this.options.duibiScreenWidth * _win_width / 10, this_index = $this_obj.index(),
                this_off_left = $this_obj.offset().left, this_pos_left = $this_obj.position().left,
                this_width = $this_obj.width(), this_prev_width = $this_obj.prev("li").width(),
                this_next_width = $this_obj.next("li").width();
            let this_off_right = _win_width - this_off_left - this_width;

            if (_scroller_w + 2 > _wrapper_width) {
                if (this.options.fingerClick === 1) {
                    if (this_index === 1) {
                        myScroll.scrollTo(-this_pos_left + this_prev_width, 0, this.options.clickScrollTime);
                    } else if (this_index === 0) {
                        myScroll.scrollTo(-this_pos_left, 0, this.options.clickScrollTime);
                    } else if (this_index === _obj_li.length - 2) {
                        myScroll.scrollBy(this_off_right - _wrapper_off_right - this_width, 0, this.options.clickScrollTime);
                    } else if (this_index === _obj_li.length - 1) {
                        myScroll.scrollBy(this_off_right - _wrapper_off_right, 0, this.options.clickScrollTime);
                    } else {
                        if (this_off_left - _wrapper_off_left - (this_width * this.options.fingerClick) < duibi) {
                            myScroll.scrollTo(-this_pos_left + this_prev_width + (this_width * this.options.fingerClick), 0, this.options.clickScrollTime);
                        } else if (this_off_right - _wrapper_off_right - (this_width * this.options.fingerClick) < duibi) {
                            myScroll.scrollBy(this_off_right - this_next_width - _wrapper_off_right - (this_width * this.options.fingerClick), 0, this.options.clickScrollTime);
                        }
                    }
                } else {
                    if (this_index === 1) {
                        myScroll.scrollTo(-this_pos_left + this_prev_width, 0, this.options.clickScrollTime);
                    } else if (this_index === _obj_li.length - 1) {
                        if (this_off_right - _wrapper_off_right > 1 || this_off_right - _wrapper_off_right < -1) {
                            myScroll.scrollBy(this_off_right - _wrapper_off_right, 0, this.options.clickScrollTime);
                        }
                    } else {
                        if (this_off_left - _wrapper_off_left < duibi) {
                            myScroll.scrollTo(-this_pos_left + this_prev_width, 0, this.options.clickScrollTime);
                        } else if (this_off_right - _wrapper_off_right < duibi) {
                            myScroll.scrollBy(this_off_right - this_next_width - _wrapper_off_right, 0, this.options.clickScrollTime);
                        }
                    }
                }
            }

            $this_obj.addClass(this.options.className).siblings("li").removeClass(this.options.className);
            this.options.afterClickNav.call(this, $this_obj);
        },
    };

    let old = $.fn.navbarscroll;

    $.fn.navbarscroll = function (options) {
        return new Navbarscroll(this, options);
    };

    $.fn.navbarscroll.defaults = {
        className: "active",// 选中样式
        clickScrollTime: 300,  // 点击后滑动时间
        duibiScreenWidth: 0.4, // 单位以rem为准，默认为0.4rem
        scrollerWidth: 3,
        defaultSelect: 0, // 默认选中
        fingerClick: 0, // 点击触发
        afterClickNav: $.noop,
    }

    $.fn.navbarscroll.Constructor = Navbarscroll;

    $.fn.navbarscroll.noConflict = function () {
        $.fn.navbarscroll = old;
        return this;
    };
}));
